//
//  ALResponseView.m
//  Amily
//
//  Created by 张鹏 on 16/1/7.
//  Copyright © 2016年 Amily. All rights reserved.
//

#import "ALResponseView.h"
@interface ALResponseView ()

@property(nonatomic,strong)UIImageView *errorImg;
@property(nonatomic,strong)UILabel * errorTitle;
@property(nonatomic,strong)UILabel * errorSubTitle;
@property(nonatomic,strong)UIButton * errorButton;


@end

@implementation ALResponseView


- (instancetype)init
{
    if (self = [super init]) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickReloadData:)];
        [self addGestureRecognizer:tap];
    }
    return self;
}


- (void)configResponseView:(ALResponsePageType)page hasData:(BOOL)hasData netError:(id)error reloadBlock:(void (^)(id sender))block
{
  
    
    [self initNetWorkErrorImg];
    [self initErrorTitle];
    [self initErrorSubTitle];
    [self initErrorButton];
    
    BOOL networkError = !error || ![ALNetworkStatus networkReachable];
    

    _errorSubTitle.hidden = !networkError;
    _errorButton.hidden = !networkError;
    _reloadBlock = block;
    if (networkError) {
        _errorTitle.text = @"网络加载失败";
        
        return;
    }
    
    
    if (hasData) {
        [self removeFromSuperview];
        return;
    }
    
    switch (page) {
        case ResponsePageCommon:
            _errorTitle.text = @"页面无结果";
            break;
            
        case ResponsePageShopProducts:
            _errorTitle.text = @"没有获取到数据";
            break;
            
        case ResponsePageSearch:
            _errorTitle.text = @"没有搜索到您要的东西";
            break;
        case ResponsePageOrder:
            _errorTitle.text = @"你还没有订单哦";
            break;
        case ResponsePageFavoriteShop:
            _errorTitle.text = @"你还没有收藏店铺哦";
            break;
        case ResponsePageFavoriteProduct:
            _errorTitle.text = @"你还没有收藏商品哦";
            break;
        case ResponsePageFavoriteMaster:
            _errorTitle.text = @"你还没有收藏匠人哦";
            break;
        case ResponsePageFavoriteArticle:
            _errorTitle.text = @"你还没有收藏专题哦";
            break;
        case ResponsePageAround:
            _errorTitle.text = @"你还没开启定位,开启定位才能查看附近的店铺哦";
            break;
        case ResponsePageShopOffline:
            _errorTitle.text = @"这个店铺已下架，看看其它店铺吧~";
            break;
        case ResponsePageMasterOffline:
            _errorTitle.text = @"这个匠人已下架，看看其他匠人吧~";
            break;
        case ResponsePageShopEvents:
            _errorTitle.text = @"优惠券已被抢光了，明天再来看看吧";
            break;
        case ResponsePageNearest:
            _errorTitle.text = @"你还没开启定位，开启定位才能按照距离最近查找商品哦";
            break;
        case ResponsePageApplyOrderEvents:
            _errorTitle.text = @"您没有可用的优惠券";
            break;
        case ResponsePageArticleList:
            _errorTitle.text = @"没有找到专题";
            break;
        case ResponsePageHome:
            _errorTitle.text = @"没有找到该分类的店铺";
        default:
            break;
    }
    
}


- (void)initNetWorkErrorImg
{
    if (!_errorImg) {
        _errorImg = [[UIImageView alloc]init];
        _errorImg.image = ImageNamed(@"wujieguo");
        _errorImg.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:_errorImg];
    }
    [_errorImg mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self).with.offset(-108);
        make.centerX.equalTo(self);
        make.height.equalTo(@40);
        make.width.equalTo(@40);
        
    }];
}


- (void)initErrorTitle
{
    if (!_errorTitle) {
        _errorTitle = [[UILabel alloc]init];
        _errorTitle.text = @"网络加载失败";
        _errorTitle.textColor = ALCOLOR(@"#b4b4b5");
        _errorTitle.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
        _errorTitle.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:_errorTitle];
    }
    [_errorTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_errorImg.mas_bottom).with.offset(18);
        make.centerX.equalTo(self);
    }];
}


- (void)initErrorSubTitle
{
    if (!_errorSubTitle) {
        _errorSubTitle = [[UILabel alloc]init];
        _errorSubTitle.text = @"请再次刷新或检查网络";
        _errorSubTitle.textColor = ALCOLOR(@"#b4b4b5");
        _errorSubTitle.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        _errorSubTitle.translatesAutoresizingMaskIntoConstraints = NO;
        _errorSubTitle.hidden = YES;
        [self addSubview:_errorSubTitle];
    }
    [_errorSubTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_errorTitle.mas_bottom).with.offset(4);
        make.centerX.equalTo(self);
    }];
}



- (void)initErrorButton
{
    if (!_errorButton) {
        _errorButton = [[UIButton alloc]init];
        [_errorButton setTitle:@"点击刷新" forState:0];
        [_errorButton setTitleColor:ALCOLOR(@"#ffffff") forState:0];
        _errorButton.titleLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
        [_errorButton addTarget:self action:@selector(clickReloadData:) forControlEvents:UIControlEventTouchUpInside];
        _errorButton.translatesAutoresizingMaskIntoConstraints = NO;
        _errorButton.backgroundColor = ALCOLOR(@"#121313");
        _errorButton.hidden = YES;
        [self addSubview:_errorButton];
    }
    [_errorButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_errorSubTitle.mas_bottom).with.offset(54);
        make.centerX.equalTo(self);
        make.width.equalTo(@106);
        make.height.equalTo(@36);
    }];
}


- (void)clickReloadData:(id)sender
{
    [SVProgressHUD showWithStatus:@"加载中"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
    });
    
    if (_reloadBlock) {
        
        _reloadBlock(sender);
    }
}

@end
